<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------
namespace app\shortvideo\model;

use app\BaseModel;
use app\passenger\model\AdminActive as PassengerModel;
use app\redbag\model\AdminActive as RedbagModel;
use app\reduction\model\Active as ReductionModel;
use think\facade\Db;

class ShortvideoPreferential extends BaseModel{

    //定义表名
    protected $name = 'longbing_card_shortvideo_preferential';

    //定义关联的优惠表 根据类型获取 1.带客有礼  2.新客福包  3.满减返佣
    public function hasTable($type){
        $array = [
            1=>['table'=>'longbing_card_passenger_active','name'=>'带客有礼'],
            2=>['table'=>'longbing_card_redbag_active','name'=>'新客福包'],
            3=>['table'=>'longbing_card_reduction_active','name'=>'满减返佣']
        ];
        return  $array[$type];
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/21 13:09
     * @功能说明:关联带客有礼
     */
    public function passenger(){
        return $this->hasOne(PassengerModel::class,'id','preferential_id');
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/21 13:09
     * @功能说明:关联新客福包
     */
    public function redbag(){
        return $this->hasOne(RedbagModel::class,'id','preferential_id');
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/21 13:09
     * @功能说明: 关联满减返佣
     */
    public function reduction(){
        return $this->hasOne(ReductionModel::class,'id','preferential_id');
    }

    //获取所有关联了视频的优惠id/类型
    public function getId($where){
        $list = $this->where($where)->field('preferential_id,type')->select();
        $arr = [];
        if(!empty($list)){
            foreach ($list as $item){
                $arrInfo = [];
                switch ($item['type']){
                    case 1: //带客有礼
                        $info = $item->passenger->toArray();
                        if(!empty($info)){
                            $arrInfo = [
                                'type_name' => '带客有礼',
                                'name' => $info['title'],
                                'start_time' => $info['start_time'],
                                'end_time' => $info['end_time']
                            ];
                        }
                        break;
                    case 2: //新客福包
                        $info = $item->redbag->toArray();
                        if(!empty($info)){
                            $arrInfo = [
                                'type_name' => '新客福包',
                                'name' => $info['title'],
                                'use_condition' => $info['use_condition'],
                                'user_num' => $info['user_num'],
                            ];
                        }
                        break;
                    case 3: //满减返佣
                        $info = $item->reduction->toArray();
                        if(!empty($info)){
                            $arrInfo = [
                                'type_name' => '满减返佣',
                                'name' => $info['title'],
                                'full' => $info['full'], //满多少
                                'reduction' => $info['reduction'], //减多少
                                'cash' => $info['cash'] //反佣多少
                            ];
                        }
                        break;
                    default:
                }
                if(!empty($info)){
                    $arr[] = [
                        'id'  => $info['id'],
                        'type'=> $item['type'],
                        'info'=> $arrInfo
                    ];
                }

            }
        }

        return $arr;
    }

    /**
     * @param $dis
     * @param $data
     * @return int
     * 编辑
     */
    public function preferentialVideoAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
        return $res;
    }

    /**
     * @param $where
     * @param $data
     * @return int
     * 删除
     */
    public function preferentialVideoDel($where,$data){
        $res = $this->where($where)->where('id','not in',$data)->delete();
        return $res;
    }

    /**
     * @param $where
     * @return int
     * 查询
     */
    public function preferentialVideoFind($where){
        $data = $this->where($where)->find();
        return $data;
    }

}